import {Component, OnInit} from '@angular/core';
import {Page} from '../../../public/util/page';
import {NewsService} from '../news.service';
import {Enums, NewsStates, States} from '../../../public/setting/enums';
import {SettingUrl} from '../../../public/setting/setting_url';

@Component({
  selector: 'app-news-list',
  templateUrl: './news-list.component.html',
  styleUrls: ['./news-list.component.scss']
})
export class NewsListComponent implements OnInit {
  public searchParams: any = {};//搜索参数
  public newsList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public states = Enums.newsState;       // 状态
  public newsStates = NewsStates;

  constructor(private newsService: NewsService) {
  }

  ngOnInit() {
    this.queryNewsList();
  }

  /**
   * 查询news列表
   * @param curPage 当前页
   */
  queryNewsList(curPage?: number) {
    this._loading = true;
    if (curPage) this.newsList.curPage = curPage;//当有页码时，查询该页数据
    this.newsList.params = {
      curPage: this.newsList.curPage, //目标页码
      pageSize: this.newsList.pageSize, //每页条数
    };
    this.newsService.getNewsList(this.newsList.params).then((res: Page) => {
      this._loading = false;
      this.newsList = res;
    }).catch(err => {
      this._loading = false;
    });
  }

  delete(code) {
    this.newsService.deleteNews(code).then(res => {
      this.queryNewsList();
    });
  }

  /**
   * 重置搜索
   */
  resetSearch() {
    this.searchParams = {};
    this.queryNewsList(1);
  }
}
